package com.swordget.service.impl;


import com.swordget.dao.Sys_DataSourceDao;
import com.swordget.entity.Sys_DataSource;
import com.swordget.entity.vo.SimplePageRequestVo;
import com.swordget.entity.vo.SimplePageResponseVo;
import com.swordget.service.Sys_DataSourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;

@Service
public class Sys_DataSourceServiceImpl implements Sys_DataSourceService {

    private final static Logger LOG = LoggerFactory.getLogger(Sys_DataSourceServiceImpl.class);

    @Resource
    private Sys_DataSourceDao sysDataSourceDao;


    @Override
    public SimplePageResponseVo findAll(SimplePageRequestVo pageVo) {
        return sysDataSourceDao.findAll(pageVo);
    }

    @Override
    public List<Sys_DataSource> findAll() {
        return sysDataSourceDao.findAll();
    }

    @Override
    public boolean addSys_dataSource(Sys_DataSource sys_DataSource) {
        return sysDataSourceDao.addSys_dataSource(sys_DataSource);
    }

    @Override
    public boolean editSys_dataSource(Sys_DataSource sys_DataSource) {
        return sysDataSourceDao.editSys_dataSource(sys_DataSource);

    }

    @Override
    public String testSys_dataSource(Sys_DataSource sys_DataSource) {
        String jdbcUrlBase = "jdbc:oracle:thin:@//%s:%s/%s";
        String jdbcUrl = String.format(jdbcUrlBase, sys_DataSource.getIpAddress(), sys_DataSource.getPortNumber(),
                sys_DataSource.getDatabaseName());
        String username = sys_DataSource.getUserName();
        String password = sys_DataSource.getPassword();

        try {
            // 加载数据库驱动
            Class.forName("oracle.jdbc.OracleDriver");

            // 建立连接
            Connection connection = DriverManager.getConnection(jdbcUrl, username, password);

            // 测试连接是否成功
            if (connection != null && !connection.isClosed()) {
                // 关闭连接
                connection.close();
                return "数据库连接成功！";
            } else {
                return "数据库连接失败！";
            }
        } catch (ClassNotFoundException e) {
            LOG.error("程序执行过程中出现异常", e);
            return "数据库驱动未找到！";
        } catch (SQLException e) {
            LOG.error("程序执行过程中出现异常", e);
            return "数据库连接异常！";
        }
    }

    @Override
    public Connection createConnection(String id) {
        Sys_DataSource sys_DataSource = sysDataSourceDao.flindOne(id);
        Connection conn = null;
        if (Objects.nonNull(sys_DataSource)) {
            String jdbcUrlBase = "jdbc:oracle:thin:@//%s:%s/%s";
            String jdbcUrl = String.format(jdbcUrlBase, sys_DataSource.getIpAddress(), sys_DataSource.getPortNumber(),
                    sys_DataSource.getDatabaseName());
            String username = sys_DataSource.getUserName();
            String password = sys_DataSource.getPassword();
            try {
                // 加载数据库驱动
                Class.forName("oracle.jdbc.OracleDriver");

                // 建立连接
               conn = DriverManager.getConnection(jdbcUrl, username, password);
            } catch (ClassNotFoundException e) {
                LOG.error("程序执行过程中出现异常", e);
            } catch (SQLException e) {
                LOG.error("程序执行过程中出现异常", e);
            }
        }
        return conn;
    }
}
